 package com.oanda.blackberry.view;
 
 import com.oanda.blackberry.net.ConnectionManager;
 import com.oanda.blackberry.restapi.User;
 import com.oanda.blackberry.util.AbstractController;
 import com.oanda.blackberry.util.AbstractViewPanel;
 import com.oanda.blackberry.util.DefaultController;
 import com.oanda.blackberry.util.Model;
 import com.oanda.blackberry.util.PropertyChangeEvent;
 import com.oanda.blackberry.util.Settings;
 import com.oanda.blackberry.util.log.Logger;
 import net.rim.device.api.system.Application;
 import net.rim.device.api.ui.Field;
 import net.rim.device.api.ui.FieldChangeListener;
 import net.rim.device.api.ui.Font;
 import net.rim.device.api.ui.Manager;
 import net.rim.device.api.ui.MenuItem;
 import net.rim.device.api.ui.Screen;
 import net.rim.device.api.ui.UiApplication;
 import net.rim.device.api.ui.component.ButtonField;
 import net.rim.device.api.ui.component.LabelField;
 import net.rim.device.api.ui.component.Menu;
 import net.rim.device.api.ui.component.NullField;
 import net.rim.device.api.ui.container.HorizontalFieldManager;
 import net.rim.device.api.ui.container.MainScreen;
 
 public class MenuView extends MainScreen
   implements AbstractViewPanel
 {
   DefaultController controller;
   ButtonField rates = new ButtonField("Rates", 65536L);
   ButtonField trades = new ButtonField("Trades", 65536L);
   ButtonField orders = new ButtonField("Orders", 65536L);
   ButtonField positions = new ButtonField("Positions", 65536L);
   static final String TITLE = "Main Menu";
   HorizontalFieldManager hStatus = new HorizontalFieldManager();
   LabelField statusLabel = new LabelField();
 
   MenuItem accountsMenuItem = new MenuItem("Accounts", 1000, 19997)
   {
     public void run()
     {
       MenuView.this.controller.propertyChange(new PropertyChangeEvent(this, "acountList", null, MenuView.this.controller));
       MenuView.this.controller.accountList(Model.getModel().getSessionToken(), Model.getModel().getUser().getUsername());
     }
   };
 
   MenuItem logoutMenuItem = new MenuItem("Log out", 1000, 20000)
   {
     public void run()
     {
       MenuView.this.hide();
       MenuView.this.controller.logout(Model.getModel().getSessionToken());
     }
   };
 
   MenuItem optionsMenuItem = new MenuItem("Options", 1000, 20001)
   {
     public void run()
     {
       MenuView.this.controller.showOptions();
     }
   };
 
   MenuItem helpMenuItem = new MenuItem("Contact", 1000, 20002)
   {
     public void run()
     {
       MenuView.this.controller.help();
     }
   };
 
   MenuItem aboutMenuItem = new MenuItem("About", 1000, 20003)
   {
     public void run()
     {
       MenuView.this.controller.about();
     }
   };
 
   MenuItem debugMenu = new MenuItem("Debug Menu", 1000, 20004)
   {
     public void run()
     {
       MenuView.this.controller.propertyChange(new PropertyChangeEvent(this, "showMenuView", MenuView.this.controller, MenuView.this.controller));
     }
   };
 
   MenuItem closeMenu = new MenuItem("Exit", 1000, 20005)
   {
     public void run()
     {
       MenuView.this.onClose();
     }
   };
 
   public MenuView(DefaultController controller)
   {
     this.controller = controller;
     setTitle("Main Menu");
     add(this.rates);
     add(this.trades);
     add(this.orders);
     add(this.positions);
 
     this.rates.setChangeListener(new FieldChangeListener() {
 
       public void fieldChanged(Field field, int context) { MenuView.this.controller.propertyChange(new PropertyChangeEvent(this, "requestShowRatesView", MenuView.this.controller, MenuView.this.controller));
       }
     });
     this.trades.setChangeListener(new FieldChangeListener() {
 
       public void fieldChanged(Field field, int context) { MenuView.this.controller.propertyChange(new PropertyChangeEvent(this, "requestShowTradesView", MenuView.this.controller, MenuView.this.controller));
       }
     });
     this.orders.setChangeListener(new FieldChangeListener() {
 
       public void fieldChanged(Field field, int context) { MenuView.this.controller.propertyChange(new PropertyChangeEvent(this, "requestShowOrdersView", MenuView.this.controller, MenuView.this.controller));
       }
     });
     this.positions.setChangeListener(new FieldChangeListener() {
 
       public void fieldChanged(Field field, int context) { MenuView.this.controller.propertyChange(new PropertyChangeEvent(this, "requestShowPositionsView", MenuView.this.controller, MenuView.this.controller));
       }
     });
     this.hStatus.add(this.statusLabel);
     this.statusLabel.setFont(DefaultController.getComponentFont().derive(0, 14));
   }
 
   protected void onUiEngineAttached(boolean attached) {
     if (attached) {
       this.controller.propertyChange(new PropertyChangeEvent(null, "mainMenuShown", null, null));
       String accountId = Model.getModel().getActiveAccountId();
       setNewTitle(ConnectionManager.getUrlFriendlyName() + (accountId != null ? " - " + accountId : ""));
     }
   }
 
   public void modelPropertyChange(PropertyChangeEvent evt)
   {
     if (evt.getPropertyName().equals("showMainMenuView")) {
       this.controller.pushScreen(this);
     }
     else if (evt.getPropertyName().equals("removeMenuStatus")) {
       removeStatusField();
     }
     else if (evt.getPropertyName().equals("setMenuStatus"))
       setStatusField((String)evt.getNewValue());
   }
 
   private void setNewTitle(final String title)
   {
     Runnable r = new Runnable() {
       private final String _title = title;
 
       public void run() { MenuView.this.setTitle("Main Menu (" + this._title + ")");
       }
     };
     this.controller.invokeLater(r);
   }
 
   protected void makeMenu(Menu menu, int instance) {
     menu.add(this.debugMenu);
     menu.add(this.accountsMenuItem);
     menu.add(this.optionsMenuItem);
     menu.add(this.logoutMenuItem);
     menu.add(this.helpMenuItem);
     menu.add(this.aboutMenuItem);
     menu.add(this.closeMenu);
   }
 
   public boolean onClose()
   {
     Logger.log("onClose");
     Model.getModel().getSettings().commit();
 
     System.exit(0);
     return true;
   }
 
   private void show()
   {
     this.controller.pushScreen(this);
   }
 
   private void hide() {
     this.controller.popScreen(this);
   }
 
   private void setStatusField(final String message) {
     UiApplication.getUiApplication().invokeLater(new Runnable() {
       private final String _message=message;
 
       public void run() { MenuView.this.statusLabel.setText(this._message);
         MenuView.this.setStatus(MenuView.this.hStatus); }
     });
   }
 
   private void removeStatusField() {
     UiApplication.getUiApplication().invokeLater(new Runnable()
     {
       public void run() {
         MenuView.this.setStatus(new NullField());
       }
     });
   }
}

